﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BizTalk.BuildGenerator.UI.Navigation;

namespace BizTalk.BuildGenerator.UI.UserControls
{
    public partial class GenerationOptionsControl : UserControl, IWizardControl
    {
        private GenerationArgs _Args;

        public GenerationOptionsControl()
        {
            InitializeComponent();
        }

        private void GenerationOptionsControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                foreach (string provider in BuildGenerator.Configuration.ConfigurationFacade.BuildProviders)
                    BuildProviderComboBox.Items.Add(provider);

                if (this._Args != null && this._Args.GenerationProvider != null)
                {
                    if (BuildProviderComboBox.Items.Contains(this._Args.GenerationProvider))
                        BuildProviderComboBox.SelectedText = this._Args.GenerationProvider;
                }
            }
        }
        /// <summary>
        /// Registers the args for use by this control
        /// </summary>
        /// <param name="args"></param>
        public void RegisterArgs(GenerationArgs args)
        {
            this._Args = args;
            
        }

        #region IWizardControl Members
        /// <summary>
        /// Implements move next
        /// </summary>
        /// <returns></returns>
        public MoveNextResult MoveNext()
        {
            MoveNextResult result = new MoveNextResult();
            if (string.IsNullOrEmpty(BuildProviderComboBox.Text))
            {
                result.CanMoveNext = false;
                result.AddError("Please select a build provider");                
                return result;
            }

            Type providerType = Type.GetType(BuildProviderComboBox.SelectedText, false, true);
            if (providerType == null)
            {
                result.CanMoveNext = false;
                result.AddError("The type of the selected provider can not be found.  Please check its assembly");
                return result;
            }

            if (providerType.IsAssignableFrom(typeof(IBuildProvider)))
            {
                result.CanMoveNext = false;
                result.AddError("The type of the selected provider does not implement the correct interface");
                return result;
            }
            result.CanMoveNext = true;
            this._Args.GenerationProvider = BuildProviderComboBox.SelectedText;

            return result;
        }

        #endregion
    }
}
